package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.order.client.OrderInfoFeignClient;
import com.atguigu.tingshu.search.repository.AlbumInfoRepository;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.search.service.MqOpsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Optional;

@Service
public class MqOpsServiceImpl implements MqOpsService {

    @Autowired
    private ItemService itemService;

    @Autowired
    @Qualifier("com.atguigu.tingshu.album.client.AlbumInfoFeignClient")
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    @Qualifier("com.atguigu.tingshu.order.client.OrderInfoFeignClient")
    private OrderInfoFeignClient orderInfoFeignClient;

    @Autowired
    private AlbumInfoRepository albumInfoRepository;

    @Override
    public void listenOnSaleAlbum(String albumId) {

        itemService.onAlbumEs(Long.parseLong(albumId));
    }

    @Override
    public void listenOffSaleAlbum(String albumId) {
        itemService.offAlbumEs(Long.parseLong(albumId));
    }

    /**
     * 更新专辑购买数量
     * @param orderNo
     */
    @Override
    public void updateAlbumBuyNum(String orderNo) {

        // 1.根据订单编号查询订单信息获取订单类型
        OrderInfo orderInfoData = orderInfoFeignClient.getOrderInfoByOrderNo(orderNo).getData();
        Assert.notNull(orderInfoData, "远程查询订单信息失败");
        // 2.获取订单的类型
        String itemType = orderInfoData.getItemType();
        //初始化专辑id
        Long albumId = null;
        //3.判断购买的商品类型
        // 3.1 如果是专辑订单，获取专辑id
        if(SystemConstant.ORDER_ITEM_TYPE_ALBUM.equals(itemType)){
            albumId = orderInfoData.getOrderDetailList().get(0).getItemId();
        }
        // 3.2 如果是声音订单，获取声音所在的专辑id
        if(SystemConstant.ORDER_ITEM_TYPE_TRACK.equals(itemType)){
            Long trackId = orderInfoData.getOrderDetailList().get(0).getItemId();
            TrackInfo trackInfoData = albumInfoFeignClient.getTrackInfoById(trackId).getData();
            Assert.notNull(trackInfoData, "远程查询声音信息失败");
            albumId = trackInfoData.getAlbumId();
        }
        // 3.3 如果是会员订单，es不做处理
        if(SystemConstant.ORDER_ITEM_TYPE_VIP.equals(itemType)){
            return;
        }

        if(albumId != null){
            //修改购买量
            Optional<AlbumInfoIndex> albumInfoIndex = albumInfoRepository.findById(albumId);
            if(albumInfoIndex.isPresent()){
                AlbumInfoIndex albumInfoIndexData = albumInfoIndex.get();
                albumInfoIndexData.setBuyStatNum(albumInfoIndexData.getBuyStatNum() + orderInfoData.getOrderDetailList().size());
                albumInfoRepository.save(albumInfoIndexData);
            }
        }
    }
}